package org.ssc.audience.controller;


import org.ssc.audience.entity.Order;
import org.ssc.audience.entity.Tickethouse;
import org.ssc.audience.entity.Audience;
import org.ssc.audience.entity.AudienceTicket;
import org.ssc.audience.feign.TickethouseFeign;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/audience")
public class AudienceHandler {

    @Resource
    TickethouseFeign tickethouseFeign;

    /**
     * 观众注册
     * @param audience json格式见resources目录
     * @return
     */
    @PostMapping("/audienceRegister")
    public String audienceRegister(@RequestBody  Audience audience){
        Audience a = tickethouseFeign.findAudience(audience.getAname());
        if(a != null){
            return "该用户已存在";
        }else{
            int aid = tickethouseFeign.audienceRegister(audience);
            if(aid == 1){
                return "注册成功";
            }else{
                return "注册失败";
            }
        }
    }

    /**
     * 观众登录
     * @param aname 用户名
     * @param password 密码
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String login(@RequestParam("aname") String aname,
                        @RequestParam("password") String password,HttpSession session){
        Object object = tickethouseFeign.audienceLogin(aname,password);
        if(object != null){
            LinkedHashMap<String, Object> hashMap = (LinkedHashMap) object;
            Audience audience = new Audience();
            audience.setAid((int)hashMap.get("aid"));
            session.setAttribute("audience",audience);
            return "观众登录成功";
        }else{
            return "观众登录失败";
        }
    }

    /**
     * 登出
     * @param session 清除session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "登出";
    }

    /**
     * 查询已购电影票(需要登录)
     * @param session
     * @return 返回购票列表
     */
    @GetMapping("/getOrderByAid")
    public Object getOrderByAid(HttpSession session){
        Audience a = (Audience) session.getAttribute("audience");
        if(a == null) {
           return "请先登录";
        }else{
            List<Order> orderList = new ArrayList<>();
            orderList = tickethouseFeign.getOrderByAid(a.getAid());
            return orderList;
        }
    }

    /**
     * 购票
     * @param tid 电影场次id
     * @param session
     * @return
     */
    @GetMapping("/buyTicket")
    public String buyTicket(@RequestParam("tid") int tid,HttpSession session){
        Audience a = (Audience) session.getAttribute("audience");
        if(a == null) {
            return "请先登录";
        }
        int result = tickethouseFeign.buyTicket(tid,a.getAid());
        if (result == 1){return "购票成功";}
        else{return "购票失败";}
    }

    /**
     * 查询所有电影场次信息
     * @return 返回票仓list
     */
    @GetMapping("getAllTickets")
    public List<Tickethouse> getAllTickets(){
        List<Tickethouse> tickethouseList = new ArrayList<>();
        tickethouseList = tickethouseFeign.getAllTickets();
        return tickethouseList;
    }

    /**
     * 根据电影名称查询电影场次信息（返回字段与cinema不同）
     * @param moviename
     * @return
     */
    @GetMapping("getTicketsByMoviename/{moviename}")
    public  List<AudienceTicket> getTicketsByMoviename(@PathVariable("moviename") String moviename){
        List<Tickethouse> tickethouseList = new ArrayList<>();
        tickethouseList = tickethouseFeign.getTicketsByMoviename(moviename);
        List<AudienceTicket> audienceTicketList = new ArrayList<>();
        if(tickethouseList.size() != 0) {
            for (Tickethouse t:tickethouseList) {
                AudienceTicket a = new AudienceTicket();
                a.setCid(t.getCid());
                a.setMoviename(t.getMoviename());
                a.setDate(t.getDate());
                a.setNumber(t.getNumber());
                a.setHall(t.getHall());
                a.setPrice(t.getPrice());
                audienceTicketList.add(a);
            }
        }
        return audienceTicketList;
    }
}
